import { Component } from '@angular/core';
import {HttpClient} from "@angular/common/http";

import {CookieService} from 'ngx-cookie-service'


@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'zangjun';
  authenticated = false;
  credentials={username:'zangchuanjun',password:'123456'};
  order={};

  constructor(private http:HttpClient,private cookie:CookieService){
    this.http.get('/api/user/me').subscribe(data=>{
      if(data){
        this.authenticated=true;
      }
      if(!this.authenticated){
        window.location.href='http://auth.test.com:8085/oauth/authorize?'+
          'client_id=admin&'  //哪个客户端应用跳转过来的
          +'redirect_uri=http://admin.test.com:8080/oauth/callback&'   //回调地址，认证服务器发送回来的授权码接收地址
          +'response_type=code&'
          +'state=abc';
      }
    })


  }

  login(){
    this.http.post('login',this.credentials).subscribe(()=>{
      this.authenticated=true;
    },()=>{
      alert('login fail')
    })
  }

  logout(){
    this.cookie.delete('access_token','/','test.com')
    this.cookie.delete('refresh_token','/','test.com')
    this.http.post('logout',this.credentials).subscribe(()=>{
      window.location.href='http://auth.test.com:8085/logout?redirect_uri=http://admin.test.com:8080'
//      this.authenticated=false;
    },()=>{
      alert('logout fail');
    })
  }

  getOrder(){
    this.http.get('api/order/order/1').subscribe(data=>{
      console.log(data);
      this.order=data;
    },()=>{
      alert("get order fail")
    })
  }
}
